// 生成一个随机的UUID作为动态值
export const UUID = (len?: number, radix?: number) => {
	const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	const uuid: any[] = [];
	let i: any = 0;
	radix = radix || chars.length;
	if (len) {
			for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
	} else {
			let r;
			// 中间用特殊字符链接
			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '';
			uuid[14] = '4';
			// 长度不长于32位
			for (i = 0; i < 32; i++) {
					if (!uuid[i]) {
							r = 0 | (Math.random() * 16);
							uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
					}
			}
	}
	return uuid.join('');
};


// 生成时间戳或时间
const formatNumber = (n: any) => {
    n = n.toString();
    return n[1] ? n : `0${n}`;
};
export const formatTime = (date: { getFullYear: () => any; getMonth: () => number; getDate: () => any; getHours: () => any; getMinutes: () => any; getSeconds: () => any }, type: any) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();

	switch (type) {
			case 'YY':
					return `${[year].map(formatNumber).join('-')}`;
			case 'MM':
					return `${[year, month].map(formatNumber).join('-')}`;
			case 'DD':
					return `${[year, month, day].map(formatNumber).join('-')}`;
			case 'HH':
					return `${[year, month, day].map(formatNumber).join('-')} ${[hour].map(formatNumber).join(':')}`;
			case 'mm':
					return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute].map(formatNumber).join(':')}`;
			case 'ss':
					return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`;
			case 'HHmmss':
					return `${[hour, minute, second].map(formatNumber).join(':')}`;
			case 'unix':
					const unix = `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`;
					return Math.round(new Date(unix).getTime() / 1000);
			default:
					return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`;
	}
};

// 按ascii码从小到大排序
const sort_ascii = (obj: any) => {
	let arr = [];
	let num = 0;
	for (let i in obj) {
			arr[num] = i;
			num++;
	}
	let sortArr = arr.sort();
	//let sortObj = {};    //完成排序值
	let str = ''; //自定义排序字符串
	for (let i in sortArr) {
			str += sortArr[i] + '=' + obj[sortArr[i]] + '&';
			//sortObj[sortArr[i]] = obj[sortArr[i]];
	}
	//去除两侧字符串
	let char = '&';
	str = str.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');

	return str;
	//return sortObj;
};

// 获取app当前页面对应的url
function getConfigUrl() {
    let u = window.navigator.userAgent;
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    //安卓需要使用当前URL进行微信API注册（即当场调用location.href.split('#')[0]）
    //iOS需要使用进入页面的初始URL进行注册，（即在任何pushstate发生前，调用location.href.split('#')[0]）
    let url = '';
    if (isiOS) {
        url = `${window.localStorage.getItem('_iosWXConfig_') || window.location.href.split('#')[0]}`; //获取初始化的url相关参数
    } else {
        url = window.location.href.split('#')[0];
    }
    return url;
}


import { reactive } from 'vue';
const gridParams = reactive<any>({
	timestamp: formatTime(new Date(), 'unix') + '',
	noncestr: UUID(),
	agentid: '',
	corpid: '',
	corpsecret: '',
	jsApiList: ['checkJsApi', 'chooseImage'],
});